package com.panda.game.xls.config;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.panda.game.xls.constant.ScreenConstant;

import java.util.ArrayList;
import java.util.List;

public class PersonConfig extends IOFileLoadRunner {

    private static List<Person> personList = null;

    //初始化入口
    private static void init() {
        personList = new ArrayList();
        JSONObject json = loadExtJarJsonFile(ScreenConstant.PERSONFILE_PATH);
        initPersonList(json);
    }

    //初始化list
    private static void initPersonList(JSONObject json) {
        if (json != null) {
            JSONArray jsonArry = json.getJSONArray("persons");
            if (jsonArry != null && jsonArry.size() > 0) {
                for (int i = 0; i < jsonArry.size(); i++) {
                    JSONObject j = (JSONObject) jsonArry.get(i);
                    Person p = new Person();
                    p.setName(j.getString("name"));
                    p.setSeq(j.getInteger("seq"));
                    personList.add(p);
                }
            }
        }
    }

    public static List<Person> getPersonList() {
        if (personList == null) {
            init();
        }
        return personList;
    }

    public static void setPersonList(List<Person> personList) {
        JSONArray arr = new JSONArray();
        for (int i = 0; i < personList.size(); i++) {
            arr.set(i, personList.get(i));
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("persons", arr);
        try {
            setExtJarJsonFile(ScreenConstant.PERSONFILE_PATH, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void setList(List<Person> list) {
        personList = list;
    }

}
